package Interface.Panel;

import Controller.InputController;
import Controller.UsersController;
import Entity.User;
import Interface.Windows.MainWindow;
import java.util.ArrayList;
import javax.persistence.EntityManagerFactory;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 *
 * @author Esteban
 */
public class UsersListPanel extends javax.swing.JPanel {

    private UsersController usersController;
    private InputController validator;
    private String confirmDeleteMessage = "\"Desea eliminar este usuario";
    private String noElementSelectedMessage = "\"Por favor seleccione un usuario de la lista";
    private ArrayList<User> usersList;
    private EntityManagerFactory emf;
    private MainWindow mainWindow;

    /**
     * Creates new form UsersListPanel
     */
    public UsersListPanel(EntityManagerFactory emf, MainWindow mainWindow) {
        this.emf = emf;
        this.mainWindow = mainWindow;
        usersController = new UsersController(emf);
        validator = new InputController();
        usersList = new ArrayList<>();
        initComponents();
        usersList = usersController.findAllUsers();
        initList();
        this.deleteButton.setEnabled(false);
        this.updateButton.setEnabled(false);
        this.searchButtonGroup.add(this.searchByCedulaRadioButton);
        this.searchButtonGroup.add(this.searchByNameRadioButton);
        this.searchByNameRadioButton.setSelected(true);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        searchButtonGroup = new javax.swing.ButtonGroup();
        middlePanel = new javax.swing.JPanel();
        searchLabel = new javax.swing.JLabel();
        searchTextField = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        usersJList = new javax.swing.JList();
        updateButton = new javax.swing.JButton();
        deleteButton = new javax.swing.JButton();
        searchByCedulaRadioButton = new javax.swing.JRadioButton();
        searchByNameRadioButton = new javax.swing.JRadioButton();
        jPanel20 = new javax.swing.JPanel();
        logoLabel19 = new javax.swing.JLabel();
        titleLabel19 = new javax.swing.JLabel();

        middlePanel.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        searchLabel.setText("Buscar Empleados");

        searchTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                searchTextFieldKeyTyped(evt);
            }
        });

        usersJList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                usersJListMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(usersJList);

        updateButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Interface/Resources/update.png"))); // NOI18N
        updateButton.setText("Actualizar");
        updateButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                updateButtonMouseClicked(evt);
            }
        });

        deleteButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Interface/Resources/Cancelar.png"))); // NOI18N
        deleteButton.setText("Eliminar");
        deleteButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                deleteButtonMouseClicked(evt);
            }
        });

        searchByCedulaRadioButton.setText("Cedula");

        searchByNameRadioButton.setText("Nombre");

        javax.swing.GroupLayout middlePanelLayout = new javax.swing.GroupLayout(middlePanel);
        middlePanel.setLayout(middlePanelLayout);
        middlePanelLayout.setHorizontalGroup(
            middlePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, middlePanelLayout.createSequentialGroup()
                .addGroup(middlePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(middlePanelLayout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(searchByNameRadioButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(searchByCedulaRadioButton))
                    .addGroup(middlePanelLayout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(updateButton, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(deleteButton, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(middlePanelLayout.createSequentialGroup()
                        .addGap(81, 81, 81)
                        .addComponent(searchLabel)
                        .addGap(18, 18, 18)
                        .addComponent(searchTextField)))
                .addGap(73, 73, 73))
            .addGroup(middlePanelLayout.createSequentialGroup()
                .addGap(81, 81, 81)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 343, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(65, Short.MAX_VALUE))
        );
        middlePanelLayout.setVerticalGroup(
            middlePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(middlePanelLayout.createSequentialGroup()
                .addGap(13, 13, 13)
                .addGroup(middlePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(searchLabel)
                    .addComponent(searchTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(middlePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(searchByCedulaRadioButton)
                    .addComponent(searchByNameRadioButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 195, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(middlePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(updateButton)
                    .addComponent(deleteButton))
                .addContainerGap(24, Short.MAX_VALUE))
        );

        logoLabel19.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Interface/Resources/Empleado.png"))); // NOI18N

        titleLabel19.setFont(new java.awt.Font("Tahoma", 1, 36)); // NOI18N
        titleLabel19.setText("Buscar Empleados");

        javax.swing.GroupLayout jPanel20Layout = new javax.swing.GroupLayout(jPanel20);
        jPanel20.setLayout(jPanel20Layout);
        jPanel20Layout.setHorizontalGroup(
            jPanel20Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel20Layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addComponent(logoLabel19, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(titleLabel19)
                .addContainerGap(19, Short.MAX_VALUE))
        );
        jPanel20Layout.setVerticalGroup(
            jPanel20Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel20Layout.createSequentialGroup()
                .addGap(38, 38, 38)
                .addComponent(titleLabel19, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
            .addGroup(jPanel20Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(logoLabel19, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel20, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(middlePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel20, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(middlePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void initList() {
        DefaultListModel model = new DefaultListModel();
        for (int i = 0; i < usersList.size(); i++) {
            model.addElement(usersList.get(i).getFullName());
        }
        this.usersJList.setModel(model);
        this.usersJList.revalidate();
    }

    private void updateButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_updateButtonMouseClicked
        if (usersJList.getSelectedIndex() >= 0 && usersJList.getSelectedIndex() <= usersList.size()) {
            JFrame frame = new JFrame();
            frame.setBounds(this.getBounds());
            frame.add(new PanelUser(emf, usersList.get(usersJList.getSelectedIndex()), mainWindow));
            frame.setVisible(true);
        } else {
            JOptionPane.showMessageDialog(new JFrame(), noElementSelectedMessage, "Dialog", JOptionPane.INFORMATION_MESSAGE);
        }
    }//GEN-LAST:event_updateButtonMouseClicked

    private void usersJListMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_usersJListMouseClicked
        this.deleteButton.setEnabled(true);
        this.updateButton.setEnabled(true);
    }//GEN-LAST:event_usersJListMouseClicked

    private void searchTextFieldKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_searchTextFieldKeyTyped
        if (searchByNameRadioButton.isSelected()) {
            validator.lettersOnly(evt);
            validator.noSpecialCharacters(evt);
            usersList = usersController.findByName(this.searchTextField.getText());
        } else {
            validator.numbersOnly(evt);
            usersList = usersController.findByCedula(Integer.parseInt(this.searchTextField.getText()));
        }
        initList();
        this.deleteButton.setEnabled(false);
        this.updateButton.setEnabled(false);
    }//GEN-LAST:event_searchTextFieldKeyTyped

    private void deleteButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_deleteButtonMouseClicked
        int confirmation = JOptionPane.showConfirmDialog(new JFrame(), confirmDeleteMessage, "Dialog", JOptionPane.YES_NO_OPTION);
        if (confirmation == 0) {
            usersController.deleteUser(usersList.get(usersJList.getSelectedIndex()));
        }
    }//GEN-LAST:event_deleteButtonMouseClicked
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton deleteButton;
    private javax.swing.JPanel jPanel20;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel logoLabel19;
    private javax.swing.JPanel middlePanel;
    private javax.swing.ButtonGroup searchButtonGroup;
    private javax.swing.JRadioButton searchByCedulaRadioButton;
    private javax.swing.JRadioButton searchByNameRadioButton;
    private javax.swing.JLabel searchLabel;
    private javax.swing.JTextField searchTextField;
    private javax.swing.JLabel titleLabel19;
    private javax.swing.JButton updateButton;
    private javax.swing.JList usersJList;
    // End of variables declaration//GEN-END:variables
}
